package com.zswl.decorate.base;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zswl.decorate.api.ExceptionHandle;
import com.zswl.decorate.util.LogUtil;
import com.zswl.decorate.util.ToastUtil;
import com.zswl.decorate.widget.SelectPhotoDialog;

import java.io.File;

import static com.zswl.decorate.widget.SelectPhotoDialog.REQUEST_CODE_CAMERA;
import static com.zswl.decorate.widget.SelectPhotoDialog.REQUEST_CODE_LOCAL;

public abstract class BasePhotoActivity extends BackActivity {
    private String takePhotoPath;

    /**
     * 图片路径
     *
     * @param path
     */
    public abstract void updateHeadrImg(String path);

    /**
     * 选择头像
     */
    protected void changeHeaderImg() {
        RxPermissions permissions = new RxPermissions(this);
        // Must be done during an initialization phase like onCreate
        permissions
                .request(Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new BaseObserver<Boolean>(context) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        ToastUtil.showShortToast("请打开相机权限");
                    }

                    @Override
                    public void receiveResult(Boolean result) {
                        if (result) {
                            SelectPhotoDialog dialog = new SelectPhotoDialog(context);
                            dialog.setSingle(true);
                            dialog.setListener(new SelectPhotoDialog.SelectListener() {
                                @Override
                                public void photo(String path) {
                                    takePhotoPath = path;
                                }
                            });
                            dialog.show();
                        }
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_CODE_CAMERA) { // capture new image
                updateHeadrImg(takePhotoPath);
            } else if (requestCode == REQUEST_CODE_LOCAL) { // send local image
                if (data != null) {
                    Uri selectedImage = data.getData();
                    if (selectedImage != null) {
                        sendPicByUri(selectedImage);
                    }
                }
            }
        }

//        if (RESULT_OK == resultCode) {

            //返回图片地址集合：如果你只需要获取图片的地址，可以用这个
//            ArrayList<String> resultPaths = data.getStringArrayListExtra(EasyPhotos.RESULT_PATHS);
//            //返回图片地址集合时如果你需要知道用户选择图片时是否选择了原图选项，用如下方法获取
////            boolean selectedOriginal = data.getBooleanExtra(EasyPhotos.RESULT_SELECTED_ORIGINAL, false);
//
//            // capture new image
////                updateHeadrImg(takePhotoPath);
//            if (resultPaths != null && resultPaths.size() > 0)
//                updateHeadrImg(resultPaths.get(0));


//        }
    }

    /**
     * send image
     *
     * @param selectedImage
     */
    private void sendPicByUri(Uri selectedImage) {
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            cursor = null;

            if (picturePath == null || picturePath.equals("null")) {
                ToastUtil.showShortToast("找不到图片");
                return;
            }
            LogUtil.d(picturePath);
            updateHeadrImg(picturePath);
        } else {
            File file = new File(selectedImage.getPath());
            if (!file.exists()) {
                ToastUtil.showShortToast("找不到图片");
                return;
            }
            LogUtil.d(file.getAbsolutePath());
            updateHeadrImg(file.getAbsolutePath());

        }

    }
}
